package com.ihr360.job.core.repeat.policy;

import com.ihr360.job.core.repeat.RepeatContext;
import com.ihr360.job.core.repeat.RepeatStatus;
import com.ihr360.job.core.repeat.context.RepeatContextSupport;
import org.springframework.util.ClassUtils;

/**
 * Policy for terminating a batch after a fixed number of operations. Internal
 * state is maintained and a counter incremented, so successful use of this
 * policy requires that isComplete() is only called once per batch item. Using
 * the standard {@link RepeatTemplate} should ensure this contract is kept, but it needs
 * to be carefully monitored.
 *
 * @author Dave Syer
 *
 */
public class SimpleCompletionPolicy extends DefaultResultCompletionPolicy {

    public static final int DEFAULT_CHUNK_SIZE = 5;

    int chunkSize = 0;

    public SimpleCompletionPolicy() {
        this(DEFAULT_CHUNK_SIZE);
    }

    public SimpleCompletionPolicy(int chunkSize) {
        super();
        this.chunkSize = chunkSize;
    }

    public void setChunkSize(int chunkSize) {
        this.chunkSize = chunkSize;
    }

    /**
     * Reset the counter.
     *
     * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
     */
    @Override
    public RepeatContext start(RepeatContext context) {
        return new SimpleTerminationContext(context);
    }

    /**
     * Terminate if the chunk size has been reached, or the result is null.
     *
     * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext,
     * RepeatStatus)
     * @throws RuntimeException (normally terminating the batch) if the result is
     * itself an exception.
     */
    @Override
    public boolean isComplete(RepeatContext context, RepeatStatus result) {
        return super.isComplete(context, result) || ((SimpleTerminationContext) context).isComplete();
    }

    /**
     * Terminate if the chunk size has been reached.
     *
     * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext)
     */
    @Override
    public boolean isComplete(RepeatContext context) {
        return ((SimpleTerminationContext) context).isComplete();
    }

    /**
     * Increment the counter in the context.
     *
     * @see org.springframework.batch.repeat.CompletionPolicy#update(RepeatContext)
     */
    @Override
    public void update(RepeatContext context) {
        ((SimpleTerminationContext) context).update();
    }

    protected class SimpleTerminationContext extends RepeatContextSupport {

        public SimpleTerminationContext(RepeatContext context) {
            super(context);
        }

        public void update() {
            increment();
        }

        public boolean isComplete() {
            return getStartedCount() >= chunkSize;
        }
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return ClassUtils.getShortName(SimpleCompletionPolicy.class)+": chunkSize="+chunkSize;
    }

}
